package com.jackrain.nea.core.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.core.api.AdRefbyTableAddCmd;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.core.mapper.AdColumnMapper;
import com.jackrain.nea.core.mapper.AdRefbyTableMapper;
import com.jackrain.nea.core.mapper.MapperShortcut;
import com.jackrain.nea.core.utility.CommandData;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.query.QuerySession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.HashMap;

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class AdRefbyTableAddCmdImpl extends CommandAdapter implements AdRefbyTableAddCmd {

    @Autowired 
    private MapperShortcut mapperShortcut;
    @Autowired 
    private AdColumnMapper adColumnMapper;
    @Autowired
    private AdRefbyTableMapper adRefbyTableMapper;
    

    @Override
    @Transactional
    public ValueHolder execute(QuerySession session) throws NDSException {
        CommandData data = new CommandData(session);
        
        JSONArray jar = data.getItemData("AD_REFBYTABLE", true);
        
        JSONObject model=jar.getJSONObject(0);

        User user = session.getUser();
        int userid = user.getId();
        int ad_clientid = user.getClientId();
        int ad_orgid=user.getOrgId();


        long id = data.getObjId();
        

        model.put("AD_CLIENT_ID", ad_clientid);
        model.put("AD_ORG_ID", ad_orgid);
        model.put("AD_TABLE_ID", id);
        model.put("OWNERID", userid);
        model.put("MODIFIERID", userid);
        model.put("MODIFIEDDATE", new Timestamp(System.currentTimeMillis()));
        model.put("CREATIONDATE", new Timestamp(System.currentTimeMillis()));
        int objid=com.jackrain.nea.util.Tools.getInt(model.getString("ID"),-1);
        log.debug("AdRefbyTableAddCmdImpl id->"+objid);
        //获取参数
        long refTableId = com.jackrain.nea.util.Tools.getInt(model.getLong("AD_REFBY_TABLE_ID"),-1);
        long refColumnId = com.jackrain.nea.util.Tools.getInt(model.getLong("AD_REFBY_COLUMN_ID"),-1);
   
        if(objid<0) {
            model.put("ID", Tools.getSequence("AD_REFBYTABLE"));
            model.put("ISACTIVE", "Y");
            data.setInsertDefaultValue(model);
            mapperShortcut.insert(model, "AD_REFBYTABLE"); //插入数据
        }else{
            data.setUpdateDefaultValue(model);
            mapperShortcut.update(model, "AD_REFBYTABLE"); //更新实体
            HashMap row=adRefbyTableMapper.getrefbyid(objid);
            refTableId=refTableId<0?(Long) row.get("AD_REFBY_TABLE_ID"):refTableId;
            refColumnId=refColumnId<0?(Long) row.get("AD_REFBY_COLUMN_ID"):refColumnId;
        }

       


        //-- refby-column_id must be column of v_refby_table_id
        int count = adColumnMapper.count(refTableId, refColumnId);
        if (count == 0) {
            throw data.exception("关联表字段不存在!");
        }

        return data.success();
    }
}
